<template>
	<view class="container u-padding-bottom-0">
		<sb-back-icon />
		<view class="page-body-tab">
			<view class="bg-red pad20">
				<u-search disabled :show-action="false" searchIconColor="#080808"
					@click="$go2('/pages/main/goods/search')" placeholder="What are you looking for ..." />
			</view>
			<view class="u-wrap">
				<view class="u-menu-wrap">
					<scroll-view scroll-y scroll-with-animation class="left menu-scroll-view" :scroll-top="scrollTop">
						<view v-for="(item, index) in tabbar" :key="index" class="b-word  font24 t-c pad20 item"
							:class="{active:current == index}" @tap.stop="swichMenu(item, index)">
							{{ item.name }}
						</view>
					</scroll-view>
					<block v-for="(item, index) in tabbar" :key="index">
						<scroll-view scroll-y class="right-box bg-white" v-if="current == index">
							<view class="class-item">
								<view class="bold pad15 marv10" style="border-bottom: 1px solid #eee;">
									{{ item.name }}
								</view>
								<view class="item-container">
									<view class="thumb-box" v-for="(item1, index1) in subMenus" :key="index1"
										@click="itemClick(item1)">
										<image class="item-menu-image" :src="item1.cover" mode=""></image>
										<view class="item-menu-name u-line-2 font24 t-c b-word ">
											{{ item1.name }}
										</view>
									</view>
								</view>
							</view>
						</scroll-view>
					</block>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				tabbar: [], //左边内容
				subMenus: [], //右边内容
				currentName: "All Categories",
			};
		},
		onLoad() {
			this.getLeftMenu(null);
			this.getRightMenu();
		},
		methods: {
			// 点击左边的栏目切换
			async swichMenu(item, index) {
				this.current = index;
				// 如果为0，意味着尚未初始化
				this.getRightMenu(item.id);
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect("menu-scroll-view", "menuHeight");
					await this.getElRect("u-tab-item", "menuItemHeight");
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop =
					index * this.menuItemHeight +
					this.menuItemHeight / 2 -
					this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query
						.select("." + elClass)
						.fields({ size: true }, (res) => {
							// 如果节点尚未生成，res值为null，循环调用执行
							if (!res) {
								setTimeout(() => {
									this.getElRect(elClass);
								}, 10);
								return;
							}
							this[dataVal] = res.height;
						})
						.exec();
				});
			},

			itemClick(item) {
				uni.navigateTo({
					url: `/pages/main/goods/list?cata_id=${item.id}`,
				});
			},
			handleChangeCategory(item) {
				this.current = item.id;
				// this.active = item.id
				this.currentName = item.name;
			},
			async getMenus(pid) {
				let {
					data: { rows },
				} = await this.$get("/goodsCategory/getCategoryByPid", {
					pid,
				});
				return rows;
			},
			async getLeftMenu() {
				//左侧是顶级分类，PID=0
				let rows = await this.getMenus(null);
				rows.unshift({
					id: null,
					name: "All Categories",
				});
				console.log(rows);
				this.tabbar = rows;
			},
			async getRightMenu(id) {
				//右侧是子分类，PID=左侧选中的id
				let rows = await this.getMenus(id);
				this.subMenus = rows;
			},
		},
	};
</script>

<style lang="scss" scoped>
	.container {
		/deep/ {
			.u-tabbar__content {
				position: absolute;
			}
		}
	}

	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.left {
		width: 250rpx;
		height: 100%;

		.item {
			color: #666;

			&.active {
				font-weight: bold;
				background: #fff;
				color: #000;
				border-left: 10rpx solid $main-color;
			}
		}
	}

	.page-view {
		padding: 16rpx;
	}

	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		border-radius: 8rpx;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 33.333333%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}

	.item-menu-image {
		border-radius: 50%;
		overflow: hidden;
		width: 120rpx;
		height: 120rpx;
	}

	.container {
		.u-search-text {
			font-size: 26rpx;
			color: $u-tips-color;
			margin-left: 10rpx;
		}

		.menu {
			width: 180rpx;
			background: #f5f5f5;
			color: #666;
			position: fixed;
			height: 100vh;

			.item {
				border-left: 6rpx solid transparent;

				&.active {
					background: #fff;
					border-color: red;
					color: black;
				}
			}
		}
	}
</style>
